# -*- coding: utf-8 -*-

import string
import wordProcessor
import cPickle

class Morfologik(wordProcessor.WordProcessor) :
    "Stores and manages all morfologik entries."
    
    __mor_file_txt = 'morfologik_do_wyszukiwarek.txt'
    __mor_file_bin = 'morfologik_bin'
    __morfologik_dict = {}
    
    def __init__(self, m_file=None) :
        try :
            f = open(self.__mor_file_bin, 'r+b')
            self.__morfologik_dict = cPickle.load(f)
            f.close()
        except :
            self.__create_morfologik(m_file)
        
    def __read_morfologik(self) :
        pass
        
    def __create_morfologik(self, m_file) :
        if m_file == None :
            morfologik_file_name = self.__mor_file_txt
        else :
            morfologik_file_name = m_file
        morfologik_file = open(morfologik_file_name, 'r')
        for line in morfologik_file :
            line = line.strip()
            forms = line.split(' ')
            self.__morfologik_dict[forms[0]] = forms[1]
        morfologik_file.close()
        f = open(self.__mor_file_bin, 'w+b')
        cPickle.dump(self.__morfologik_dict, f)
        f.close()
        
    def process(self, word) :
        if word in self.__morfologik_dict : 
            return self.__morfologik_dict[word]
        else :
            return word
        
if __name__ == "__main__" :
    morfologik = Morfologik('morfologik_do_wyszukiwarek.txt')
    print morfologik.process("matematyki")

